package com.thwj.project.biz.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thwj.common.annotation.Log;
import com.thwj.common.core.controller.BaseController;
import com.thwj.common.core.domain.AjaxResult;
import com.thwj.common.core.page.TableDataInfo;
import com.thwj.common.enums.BusinessType;
import com.thwj.common.exception.ServiceException;
import com.thwj.common.utils.poi.ExcelUtil;
import com.thwj.project.biz.domain.Area;
import com.thwj.project.biz.domain.Association;
import com.thwj.project.biz.service.IAreaService;
import com.thwj.project.biz.service.IAssociationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 社群管理Controller
 *
 * @author generate
 * @date 2023-07-13
 */
@RestController
@RequestMapping("/biz/association")
public class AssociationController extends BaseController {
    @Autowired
    private IAssociationService associationService;
    @Autowired
    private IAreaService areaService;

/**
 * 查询社群管理列表
 */
@PreAuthorize("@ss.hasPermi('biz:association:list')")
@GetMapping("/list")
    public TableDataInfo list(Association association) {
        startPage();
        List<Association> list = associationService.list(new QueryWrapper<>(association));
        return getDataTable(list);
    }

    /**
     * 导出社群管理列表
     */
    @PreAuthorize("@ss.hasPermi('biz:association:export')")
    @Log(title = "社群管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Association association) {
        List<Association> list = associationService.list(new QueryWrapper<>(association));
        ExcelUtil<Association> util = new ExcelUtil<Association>(Association. class);
        util.exportExcel(response, list, "社群管理数据");
    }

    /**
     * 获取社群管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:association:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(associationService.getById(id));
    }

    /**
     * 新增社群管理
     */
    @PreAuthorize("@ss.hasPermi('biz:association:add')")
    @Log(title = "社群管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Association association) {
        // 读取
        Area area = areaService.getOne(new QueryWrapper<Area>().eq("city_code", association.getCodeCity()).last(" limit 1"),false);
        if(area == null) {
            throw new ServiceException("区域参数错误");
        }
        association.setCodeProv(area.getProCode());
        association.setNameProv(area.getProName());
        association.setNameCity(area.getCityName());
        association.setCreateDate(new Date());
        boolean flag = associationService.save(association);
        return toAjax(flag);
    }

    /**
     * 修改社群管理
     */
    @PreAuthorize("@ss.hasPermi('biz:association:edit')")
    @Log(title = "社群管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Association association) {
        return toAjax(associationService.updateById(association));
    }

    /**
     * 删除社群管理
     */
    @PreAuthorize("@ss.hasPermi('biz:association:remove')")
    @Log(title = "社群管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(associationService.removeByIds(Arrays.asList(ids)));
    }
}
